package com.focus.xyz.union.pages.backend;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/**
 * Created by chenmeng on 2016/4/13.
 */
public class XyzMainPage{
    private static final String XYZ_BACK_MAIN_URL = "http://cst.xyz.cn/";
    private Action action;
    private String url = null;

    //登录前
    @FindBy(xpath = "//*[@id=\"textfield2\"]")
    private WebElement userName;
    @FindBy(xpath = "//*[@id=\"textfield\"]")
    private WebElement password;
    @FindBy(xpath = "//*[@id=\"textfield3\"]")
    private WebElement dcode;
    @FindBy(xpath = "//img[@alt='登录']")
    private WebElement loginBtn;

    //登录后
    @FindBy(xpath = "//a[contains(text(),'[退出]')]")
    private WebElement logoutLink;
    @FindBy(xpath = "//a[contains(text(),'[进入渠道正式版]')]")
    private WebElement unionLink;



    public XyzMainPage(WebDriver driver,Action action){
        this.action = action;
        PageFactory.initElements(driver,this);
        if(StringUtils.isBlank(url=ConfigPropInfo.XYZ_BACK_MAIN_URL)){
            url = XYZ_BACK_MAIN_URL;
        }
    }


    public void openPage() throws Exception{
        action.open(url);
    }


    //登录前
    public void inputUserName(String userName) throws Exception{
        action.inputText(this.userName,userName);
    }

    public void inputPassword(String pass) throws Exception{
        action.inputText(this.password,pass);
    }

    public void inputDcode(String code) throws Exception{
        action.inputText(dcode,code);
    }

    public void clickLoginBtn() throws Exception{
        action.click(loginBtn);
    }

    public void login(String userName,String pass,String dCode) throws Exception{
        /*if(isElementExsit(By.xpath("//a[contains(text(),'[退出]')]"))){

        }else{
        }*/
        loginUseId(userName,pass,dCode);
    }

    private void loginUseId(String userName,String pass,String dCode) throws Exception{
        inputUserName(userName);
        inputPassword(pass);
        if(StringUtils.isNotBlank(dCode)){
            inputDcode(dCode);
        }
        clickLoginBtn();
    }

    public boolean loginUseCookie() throws Exception{
        //使用cookie登录
        return false;

    }

    public boolean isLogined() throws Exception{
        boolean flag = false;
        if(action.isElementExsit(By.xpath("//img[@alt='登录']"))){
            flag = false;
        }else{
            flag = true;
        }
        return flag;
    }


    //登录后
    public void linkUnion() throws Exception{
        action.click(unionLink);
        action.waitForPageLoad();
        action.switchToWindowByUrl("http://cps.xyz.cn/home/view",true);
    }
    public void logout() throws Exception{
        action.click(logoutLink);
    }

}
